// pages/qianb/qianb.js
const app = getApp();
const url = app.globalData.url;
const common = require("../../utils/common.js");
const WxParse = require('../../wxParse/wxParse.js');  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loding:true,
    czs:false,
    txs:false,
    rmb:0,
    txrmb:0,
    txsxf:0,
    viprmb:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  qrcz:function(){
    let that=this;
    that.ios();
  },
  tsk: function (tt, url) {
    let that=this;
    wx.showModal({
      title: '提示',
      content: tt,
      showCancel: true,//是否显示取消按钮
      cancelText: "否",//默认是“取消”
      cancelColor: 'skyblue',//取消文字的颜色
      confirmText: "是",//默认是“确定”
      confirmColor: 'skyblue',//确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
          that.qux();
        } else {
          //点击确定
          wx.redirectTo({
            url: url,
          })
        }
      },
      fail: function (res) { },//接口调用失败的回调函数
      complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  // 检查是否为ios
  ios: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        if (res.platform == "devtools") {
          console.log('pcios')
          that.tsk(that.data.peizi.xcxqianb.iosts.txt, "/pages/my/my");
        } else if (res.platform == "ios") {
          console.log('ios')
          that.tsk(that.data.peizi.xcxqianb.iosts.txt, "/pages/my/my");
        } else if (res.platform == "android") {
          console.log('android')
          that.pay();
        }
      }
    })
  },
  iosjc(){
      let that = this;
      wx.getSystemInfo({
        success: function (res) {
          console.log(res)
          if (res.platform == "devtools") {
            console.log('pcios')
            that.tsk(that.data.peizi.xcxqianb.iosts.txt, "/pages/my/my?ck=kf");
          } else if (res.platform == "ios") {
            console.log('ios')
            that.tsk(that.data.peizi.xcxqianb.iosts.txt, "/pages/my/my?ck=kf");
          } else if (res.platform == "android") {
            console.log('android')
            that.czs();
          }
        }
      })
  },
  qux(){
    this.setData({czs:false,txs:false,txrmb:0,txsxf:0,})
  },
  czs(){
    this.setData({czs:true})
  },
  txs(){
    this.setData({txs:true})
  },
  czhd(e){
    let peizi = this.data.peizi;
    let viprmb = e.detail.value;
    if (peizi.xcxqianb.czvipzk.txt != 1){
      viprmb = peizi.xcxqianb.czvipzk.txt * e.detail.value;
    }
    this.setData({ "rmb": e.detail.value, viprmb: viprmb.toFixed(2)});
  },
  txhd(e){
    let peizi=this.data.peizi;
    let txsxf=e.detail.value*peizi.xcxqianb.txsx.txt;
    if(this.data.peizi.xcxqianb.vipmsxf.txt==1&&this.data.user.vip==2){
      txsxf=0;
    }else{
      txsxf=txsxf.toFixed(2);
    }
    this.setData({ "txrmb": e.detail.value,txsxf:txsxf});
  },
   // 跳转url
  tourl(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
 // 配置文件
 peizi: function () {
  let that = this;
  app.fetch('api/wx/getpeizi', { dlx: 'xcxqianb' }, "GET", (err, data) => {
    console.log(data)
    if (data.code == 200) {
      if(data.data.xcxqianb.title.txt){
        wx.setNavigationBarTitle({
          title: data.data.xcxqianb.title.txt
        });
      }
      that.setData({
        peizi:data.data,rmb:data.data.xcxqianb.zscz.txt
      });
      // article_content: WxParse.wxParse('article', 'html', data.data.xcxvip.tqsm.txt, that, 0);
      that.hquser();
    } else {
      wx.showModal({
        title: '接口数据错误',
        content: '接口:"' + url + 'api/wx/getpeizi.php' + '",msg:' + data.msg,
        showCancel: false,//是否显示取消按钮
        confirmText: "好的",//默认是“确定”
        confirmColor: 'skyblue',//确定文字的颜色
        success: function (res) {

        },
        fail: function (res) { },//接口调用失败的回调函数
        complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
      })
    }
  })
},
// 获取user信息;
hquser() {
  let that = this;
  wx.getStorage({
    key: 'openid',
    success(res) {
      app.fetch('api/wx/user', { openid: res.data }, "POST", (err, data) => {
        if (data.code == 200) {
          data.data[0].viptime=common.format(data.data[0].viptime);
          that.setData({ user: data.data[0],loding:false })
          // that.getcsData();

        }
      })
    }
  })
},
  // 支付请求
  pay: function () {
    let that = this;
    that.qux();
    let user = that.data.user;
    let fee =parseInt(that.data.rmb);
    console.log(fee);
    if(user.vip==2&&that.data.peizi.xcxqianb.czvipzk.txt!=1){
      fee=fee*that.data.peizi.xcxqianb.czvipzk.txt
    }
    console.log(fee);
    fee = fee.toFixed(2);
    let sj={
      id: user.openid,
      fee: fee,
      body: (user.uname?user.uname:user.nickName) + "充值" + (that.data.rmb*that.data.peizi.xcxqianb.dhbl.txt)+"个"+that.data.peizi.xcx.jb.txt
    };
    app.fetch('api/wx/wxpay/payapi',sj, "GET", (err, data) => {
      console.log(data)
      that.setData({wxzf:data});
        wx.requestPayment({
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': function (res) {
            console.log('success');
            wx.showToast({
              title: '充值成功',
              icon: 'success',
              duration: 1000
            });
            
            that.czjb(1,that.data.rmb*that.data.peizi.xcxqianb.dhbl.txt,'充值'+ (that.data.rmb*that.data.peizi.xcxqianb.dhbl.txt)+"个"+that.data.peizi.xcx.jb.txt);
            that.addddh();
          },
          'fail': function (res) {
            console.log(res)
            wx.showToast({
              title: '充值失败 !!!',
              icon: 'none',
              duration: 2000,
              success: function () {
              }
            })
          },
          'complete': function (res) {
            console.log(res)
          }
        });
    })
  },
  czjb(sz, jb, tt, xlx){
    let that = this;
    let sj={
      uid: that.data.user.uid,
      sz: sz,
      jb: jb,
      tt: tt,
      lx:'xcx',
      xlx:xlx?xlx:''
    };
    app.fetch('api/wx/czjb', sj, "POST", (err, data) =>{
      console.log(data)
        if(data.code==200){
          that.hquser();
        }
    })
  },
  addddh(){
    let that = this;
    let fee = that.data.rmb;
    let user=that.data.user;
    if(user.vip==2&&that.data.peizi.xcxqianb.czvipzk.txt!=1){
      fee=fee*that.data.peizi.xcxqianb.czvipzk.txt
    }
    let sj={
      uid: that.data.user.uid,
      ddh: that.data.wxzf.ddh,
      rmb: fee,
      tt: '充值'+ (that.data.rmb*that.data.peizi.xcxqianb.dhbl.txt)+"个"+that.data.peizi.xcx.jb.txt
    };
    app.fetch('api/wx/addddmx', sj, "POST", (err, data) =>{
      console.log(data)
      that.qux();
    }) 
  },
  // 提现
  qrtx(){
    let that = this;
    let txje = that.data.txrmb;
    let sxf = that.data.txsxf;
    let user=that.data.user;
    let sj={
      uid: that.data.user.uid,
      txje: txje,
      sxf: sxf,
      data: (user.uname?user.uname:user.nickName)+'发起了提现申请'
    };
    app.fetch('api/wx/addtxsq', sj, "POST", (err, data) =>{
      console.log(data)
      that.czjb(2,txje*that.data.peizi.xcxqianb.dhbl.txt,'提现了'+ (txje*that.data.peizi.xcxqianb.dhbl.txt)+"个"+that.data.peizi.xcx.jb.txt,"提现："+txje+'元，手续费：'+sxf+'元,扣除');
      that.qux();
      wx.showToast({
        title: '已提交后台审核',
        icon: 'success',
        duration: 1000,
        success: function () {
          wx.navigateTo({
            url: '/pages/tixian/tixian?ck=1',
          })
        }
      });
    }) 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.peizi();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})